package admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class EditTest extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public EditTest() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ServletContext application=getServletContext();
		Connection con=(Connection) application.getAttribute("con");
		String caption=request.getParameter("caption");
		String state=request.getParameter("state");
		String time=request.getParameter("time");
		String teacher=request.getParameter("teacher");
		String student=request.getParameter("student");
		String id=request.getParameter("id");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject jot1=JSONObject.fromObject(teacher);
		JSONArray jat1=jot1.getJSONArray("teacher");
		String tj="{\"teacher\":[";
		boolean tj_first=true;
		int len1=jat1.size();
		Statement sm=null;
		try {
			sm = con.createStatement();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			for(int i=0;i<len1;++i){
				int temp_id=Integer.parseInt(jat1.getJSONObject(i).getString("id"));
				String sqls="SELECT name FROM teacher WHERE id="+temp_id;
				ResultSet rs=sm.executeQuery(sqls);
				rs.last();
				int rslen=rs.getRow();
				rs.beforeFirst();
				String result="";
				if(rslen!=1){
					out.print("编辑出错：无法找到json对应的数据库项目，请联系数据库管理员！");
				}else{
					rs.next();
					result=rs.getString(1);
					if(tj_first==true){
						tj+="{\"name\":\""+result+"\"}";
						tj_first=false;
					}else{
						tj+=",{\"name\":\""+result+"\"}";
					}
				}
			}
			tj+="]}";
		} catch (Exception e) {
			log.log.err(e.toString());
		}
		JSONObject jos1=JSONObject.fromObject(student);
		JSONArray jas1=jos1.getJSONArray("student");
		String sj="{\"student\":[";
		boolean sj_first=true;
		int len2=jas1.size();
		try {
			sm=con.createStatement();
			for(int i=0;i<len2;++i){
				int temp_id=Integer.parseInt(jas1.getJSONObject(i).getString("id"));
				String sqls="SELECT name FROM student WHERE id="+temp_id;
				ResultSet rs=sm.executeQuery(sqls);
				rs.last();
				int rslen=rs.getRow();
				rs.beforeFirst();
				String result="";
				if(rslen!=1){
					out.print("编辑出错：无法找到json对应的数据库项目，请联系数据库管理员！");
				}else{
					rs.next();
					result=rs.getString(1);
					if(sj_first==true){
						sj+="{\"name\":\""+result+"\"}";
						sj_first=false;
					}else{
						sj+=",{\"name\":\""+result+"\"}";
					}
				}
			}
			sj+="]}";
		} catch (Exception e) {
			log.log.err(e.toString());
		}
		String sqls1="UPDATE test SET caption='"+caption+"',state="+state+",time="+time+",teachers='"+tj+"',student='"+sj+"' WHERE id="+id;
		int result_sql;
		try {
			result_sql = sm.executeUpdate(sqls1);
			if(result_sql==1){
				out.print("succes");
			}else{
				out.print("数据库编辑失败！");
			}
		} catch (SQLException e) {
			log.log.err(e.toString());
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
